package org.csu.mypetstoreapi.controller;

import jakarta.servlet.http.HttpSession;
import org.csu.mypetstoreapi.common.CommonResponse;
import org.csu.mypetstoreapi.config.CorsConfig;
import org.csu.mypetstoreapi.entity.Cart;
import org.csu.mypetstoreapi.entity.CartItem;
import org.csu.mypetstoreapi.entity.Item;
import org.csu.mypetstoreapi.persistence.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;


@Controller
@RequestMapping("/cart")
@CrossOrigin
public class CartController {

    @Autowired
    private ItemMapper itemMapper;

    //购物车页面
    @PostMapping("/cartForm")
    @ResponseBody
    public CommonResponse<Cart> cartForm(@RequestBody Cart cart){
        //显示购物车信息
        return CommonResponse.createForSuccess(cart);
    }

    //item加入购物车
    @PostMapping("/addItemToCart/{workingItemId}")
    @ResponseBody
    public CommonResponse<Cart> addItemToCart(@PathVariable("workingItemId") String workingItemId,@RequestBody Cart cart){

        Item item = itemMapper.selectById(workingItemId);
        if (cart.containsItemId(workingItemId)) {
            cart.incrementQuantityByItemId(workingItemId);
        } else {
            //TODO 库存问题待解决
            cart.addItem(item, true);
            System.out.println(workingItemId+" 加入购物车......");
        }
        return CommonResponse.createForSuccess(cart);
    }

    //从购物车删除Item
    @PostMapping("removeCartItem/{workingItemId}")
    @ResponseBody
    public CommonResponse<Cart> removeCartItem(@PathVariable("workingItemId") String workingItemId, @RequestBody Cart cart){

//        cart.removeItemById(workingItemId);
        List<CartItem> cartItemList = cart.getCartItemList();
        System.out.println(cartItemList);
        //删除cartItemList中的该Item
        cartItemList.removeIf(cartItem -> Objects.equals(cartItem.getItem().getItemId(), workingItemId));
        //返回删除Item后的cart
        cart.setCartItemList(cartItemList);
        System.out.println(workingItemId+" 从购物车移除......");
        return CommonResponse.createForSuccess(cart);
    }

    @GetMapping("clearCart")
    @ResponseBody
    public CommonResponse<Cart> clearCart(@RequestBody Cart cart){

        Iterator<CartItem> cartItems = cart.getAllCartItems();
        while (cartItems.hasNext()) {
            CartItem cartItem = cartItems.next();
            String itemId = cartItem.getItem().getItemId();
            try {
                int quantity = cartItem.getQuantity();
                cart.setQuantityByItemId(itemId, quantity);
                if (quantity < 1) {
                    cartItems.remove();
                }
            } catch (Exception e) {
                //ignore parse exceptions on purpose
            }
        }

        return CommonResponse.createForSuccess(cart);
    }

    //更新购物车item数量
    @PostMapping("updateCart")
    @ResponseBody
    public CommonResponse<Cart> updateCart(@RequestBody Cart cart){

        List<CartItem> cartItemList = cart.getCartItemList();

        for(CartItem cartItem : cartItemList){
            String itemId = cartItem.getItem().getItemId();
            int quantity = cartItem.getQuantity();
            cart.setQuantityByItemId(itemId, quantity);
            if (quantity < 1) {
                cartItemList.remove(cartItem);
            }
        }
        cart.setCartItemList(cartItemList);

        return CommonResponse.createForSuccess(cart);
    }

}
